//
//  CLSegmentedView.swift
//  clreader
//
//  Created by colin on 2019/4/2.
//  Copyright © 2019 colin. All rights reserved.
//

import UIKit

class CLSegmentedView: UIView {
    
    class Item: UIView {
        lazy var labelTitle = UILabel()
        convenience init(segment:String?) {
            self.init()
            self.labelTitle.do {
                $0.text = segment
                $0.textColor = UIColor(hex: "343434")
                $0.font = UIFont.systemFont(ofSize: 15.0, weight: .bold)
            }
            self.addSubview(labelTitle)
            self.labelTitle.snp.makeConstraints({ (make) in
                make.center.equalToSuperview()
            })
        }
    }
    
    var selectedIndex = 0 {
        didSet {
            for index in 0 ..< self.items.count {
                let item = self.items[index]
                if index == self.selectedIndex {
                    item.labelTitle.textColor = UIColor(hex: "343434")
                    item.labelTitle.font = UIFont.systemFont(ofSize: 15.0, weight: .bold)
                    self.noticeLine.snp.remakeConstraints { (make) in
                        make.size.equalTo(CGSize(width: 10.0, height: 3.0))
                        make.bottom.equalToSuperview()
                        make.centerX.equalTo(item.snp.centerX)
                    }
                } else {
                    item.labelTitle.textColor = UIColor(hex: "828282")
                    item.labelTitle.font = UIFont.systemFont(ofSize: 15.0, weight: .light)
                }
            }
        }
    }
    
    private var segments = [String]()
    var items = [Item]()
    private lazy var noticeLine = UIView()
    
    convenience init(segments:[String]) {
        self.init()
        self.backgroundColor = UIColor.white
        self.segments = segments
        
        var preItem:UIView?
        for index in 0 ..< segments.count {
            
            let item = Item(segment: segments[index])
            self.addSubview(item)
            item.snp.makeConstraints { (make) in
                if let preItem_ = preItem {
                    make.left.equalTo(preItem_.snp.right)
                    make.width.equalTo(preItem_.snp.width)
                } else {
                    make.left.equalToSuperview()
                }
                make.top.bottom.equalToSuperview()
                
                if index == segments.count - 1 {
                    make.right.equalToSuperview()
                }
            }
            preItem = item
            self.items.append(item)
        }
        
        // 底部分割线
        let line = UIView()
        line.backgroundColor = UIColor(hex: "EFEFEF")
        self.addSubview(line)
        line.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(0.5)
        }
        
        self.noticeLine.backgroundColor = UIColor(hex: "AE1600")
        self.addSubview(self.noticeLine)
        self.noticeLine.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 10.0, height: 3.0))
            make.bottom.equalToSuperview()
            make.centerX.equalTo(self.items[0].snp.centerX)
        }
    }
    
    
}
